package com.mz.mattress.client.interceptor;

import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.jwt.ClientJwt;
import com.mz.mattress.api.jwt.JwtTokenUtil;
import com.mz.mattress.api.util.JacksonUtils;
import com.mz.mattress.client.common.FailMessage;
import com.mz.mattress.client.common.Holder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    private static final String UNLOGIN = JacksonUtils.toJson(Result.fail(FailMessage.UNLOGIN));

    private static final String HAS_LOCKED = JacksonUtils.toJson(Result.fail(FailMessage.HAS_LOCKED));

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String token = request.getHeader(Constants.LOGIN_TOKEN_KEY);
        if (token == null || token.isEmpty()) {

            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType(Constants.CONTENT_TYPE);
            PrintWriter printWriter = response.getWriter();
            printWriter.write(UNLOGIN);

            return false;
        }

//        UserJwt userJwt = JwtTokenUtil.getUserJwtFormToken(token);

        ClientJwt clientJwt = JwtTokenUtil.parseClientToken(token);

        if (clientJwt == null || clientJwt.getOpenId() == null || clientJwt.getMemberId() == null) {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType(Constants.CONTENT_TYPE);
            PrintWriter printWriter = response.getWriter();
            printWriter.write(UNLOGIN);

            return false;
        }

//        if (userJwt.getMerchantId() != null) {
//            log.info("merchantId=" + userJwt.getMerchantId());
//            String merchantKey = RedisKeys.getLockMerchantKey(userJwt.getMerchantId());
//            Object obj = valueOperations.get(merchantKey);
//            if (obj != null) {
//                log.info("merchantKey={},has_locked", merchantKey);
//                response.setStatus(HttpServletResponse.SC_OK);
//                response.setContentType(Constants.CONTENT_TYPE);
//                PrintWriter printWriter = response.getWriter();
//                printWriter.write(HAS_LOCKED);
//                return false;
//            }
//        }

        log.info("token_memberId=" + clientJwt.getMemberId());

//        // 这些账户 可以看见所有订单
//        Long memberId = userJwt.getMemberId();
//        for (Long id : IDS) {
//            if (id.equals(memberId)) {
//                userJwt.setMerchantId(null);
//                userJwt.setRole(Constants.ROLE_AGENT);
//            }
//        }

        //放入值
        Holder.CLIENT_JWT_THREAD_LOCAL.set(clientJwt);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        //移除值
        Holder.CLIENT_JWT_THREAD_LOCAL.remove();
    }

    public static void main(String[] args) {
        System.out.println(JacksonUtils.toJson(Result.fail(FailMessage.UNLOGIN)));
    }
}
